package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.IotRegionRpcService;
import com.hunttown.mes.rpc.domain.IotRegionDTO;
import com.hunttown.mes.rpc.domain.query.IotRegionDTOQuery;
import com.hunttown.mes.service.IotRegionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.IotRegionConverts.convert;

/**
 * created by wangjunfu on 2020-10-05 16:33:41
 */
@Service
public class IotRegionRpcServiceImpl implements IotRegionRpcService {

    private final Logger logger = LoggerFactory.getLogger(IotRegionRpcServiceImpl.class);

    private final IotRegionService iotRegionService;

    @Autowired
    public IotRegionRpcServiceImpl(IotRegionService iotRegionService) {
        this.iotRegionService = iotRegionService;
    }

    @Override
    public IotRegionDTO insert(IotRegionDTO iotRegionDTO) {
        try {
            return convert(iotRegionService.insert(convert(iotRegionDTO)));
        } catch (Exception e) {
            logger.error("IotRegion create failed.", e);
            throw new RuntimeException("IotRegion create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(IotRegionDTO iotRegionDTO) {
        try {
            return iotRegionService.updateInfoById(convert(iotRegionDTO));
        } catch (Exception e) {
            logger.error("IotRegion update by id failed.", e);
            throw new RuntimeException("IotRegion update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(IotRegionDTOQuery iotRegionDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotRegionDTOQuery.getW_id() != null && iotRegionDTOQuery.getW_id() > 0) {
                query.put("w_id", iotRegionDTOQuery.getW_id());
            }

            if (iotRegionDTOQuery.getIds() != null && !iotRegionDTOQuery.getIds().equals("")) {
                query.put("ids", iotRegionDTOQuery.getIds());
            }

            if (iotRegionDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotRegionDTOQuery.getStopFlag());
            }

            if (iotRegionDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotRegionDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(iotRegionDTOQuery.getOrderBy())) {
                query.put("orderBy", iotRegionDTOQuery.getOrderBy());
            }

            return iotRegionService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("IotRegion update by query failed.", e);
            throw new RuntimeException("IotRegion update by query failed, Please order your code.", e);
        }
    }

    @Override
    public IotRegionDTO selectById(Integer id) {
        try {
            return convert(iotRegionService.selectById(id));
        } catch (Exception e) {
            logger.error("IotRegion select by id failed.", e);
            throw new RuntimeException("IotRegion select by id failed, Please order your code.", e);
        }
    }

    @Override
    public IotRegionDTO selectById(Integer id, Integer entId) {
        return convert(iotRegionService.selectById(id, entId));
    }

    @Override
    public IotRegionDTO selectObjByQuery(IotRegionDTOQuery iotRegionDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotRegionDTOQuery.getParentId() != null) {
                query.put("parentId", iotRegionDTOQuery.getParentId());
            }

            if (StringUtils.isNotBlank(iotRegionDTOQuery.getArea())) {
                query.put("area", iotRegionDTOQuery.getArea());
            }

            if (StringUtils.isNotBlank(iotRegionDTOQuery.getBuilding())) {
                query.put("building", iotRegionDTOQuery.getBuilding());
            }

            if (StringUtils.isNotBlank(iotRegionDTOQuery.getUnit())) {
                query.put("unit", iotRegionDTOQuery.getUnit());
            }

            if (iotRegionDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotRegionDTOQuery.getStopFlag());
            }

            if (iotRegionDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotRegionDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(iotRegionDTOQuery.getOrderBy())) {
                query.put("orderBy", iotRegionDTOQuery.getOrderBy());
            }

            return convert(iotRegionService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("IotRegion select obj by query failed.", e);
            throw new RuntimeException("IotRegion select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<IotRegionDTO> getListForPage(IotRegionDTOQuery iotRegionDTOQuery) {
        Query query = Query.page(iotRegionDTOQuery.getPn(), iotRegionDTOQuery.getPageSize());

        if (iotRegionDTOQuery.getId() != null && iotRegionDTOQuery.getId() > 0) {
            query.put("id", iotRegionDTOQuery.getId());
        }

        if (iotRegionDTOQuery.getParentId() != null) {
            query.put("parentId", iotRegionDTOQuery.getParentId());
        }

        if (iotRegionDTOQuery.getIds() != null && !iotRegionDTOQuery.getIds().equals("")) {
            query.put("ids", iotRegionDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(iotRegionDTOQuery.getLikeName())) {
            query.put("likeName", iotRegionDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(iotRegionDTOQuery.getShowDate())) {
            query.put("showDate", iotRegionDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(iotRegionDTOQuery.getStartDate())) {
            query.put("startDate", iotRegionDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(iotRegionDTOQuery.getEndDate())) {
            query.put("endDate", iotRegionDTOQuery.getEndDate());
        }

        if (iotRegionDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", iotRegionDTOQuery.getStopFlag());
        }

        if (iotRegionDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", iotRegionDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(iotRegionDTOQuery.getOrderBy())) {
            query.put("orderBy", iotRegionDTOQuery.getOrderBy());
        }

        List<IotRegionDTO> list = convert(iotRegionService.selectByQuery(query));
        Page<IotRegionDTO> page = Page.from(iotRegionDTOQuery.getPn(), iotRegionDTOQuery.getPageSize(), list);
        page.setTotalItems(iotRegionService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return iotRegionService.deleteById(id);
        } catch (Exception e) {
            logger.error("IotRegion delete failed.", e);
            throw new RuntimeException("IotRegion delete failed, Please order your code.", e);
        }
    }
}